#!/bin/bash
# 该脚本测试单个文件的三个模型的加载及运行。
cd ../build/

# 记录运行时间
time_record_file="../evaluation/Time_Record.txt"
if [ ! -f $time_record_file ];then
  echo "This file is used to record the script runtime" >> $time_record_file
fi

# 先判断一下输入参数的数量是否正确，免得白跑并且不好找bug
if [ $# -ne 1 ]; then
    echo you need to specify the serial number of the ali index dataset, e.g. 1
    exit
fi

idf_num=$1
echo ${idf_num}.txt
# 加载
start_time_load=$(date +%s) # %s可以计算的是1970年以来的秒数
./read_cold -f ../db/ali_single_valid_index/${idf_num}.txt -k 16 -v 64 -d ../ssd/db_${idf_num}_k16 -m 7 -w > ../evaluation/${idf_num}_put.txt
end_time_load=$(date +%s)
cost_time_load=$((end_time_load-start_time_load))
formatted_time=$(date "+%Y-%m-%d %H:%M:%S")

# 运行：file粒度和baseline
sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
start_time_run_file=$(date +%s) # %s可以计算的是1970年以来的秒数
./read_cold -f ../db/ali_single_valid_index/${idf_num}.txt -k 16 -v 64 -d ../ssd/db_${idf_num}_k16 -m 7 -u -n 10000 -i 5 $* --change_level_load > ../evaluation/db_${idf_num}_k16_llsm.txt
end_time_run_file=$(date +%s)
cost_time_run_file=$((end_time_run_file-start_time_run_file))
start_time_run_baseline=$(date +%s) # %s可以计算的是1970年以来的秒数
./read_cold -f ../db/ali_single_valid_index/${idf_num}.txt -k 16 -v 64 -d ../ssd/db_${idf_num}_k16 -m 8 -u -n 10000 -i 5 $* --change_level_load > ../evaluation/db_${idf_num}_k16_baseline.txt
end_time_run_baseline=$(date +%s)
cost_time_run_baseline=$((end_time_run_baseline-start_time_run_baseline))

# 运行：level粒度
sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
start_time_run_level=$(date +%s) # %s可以计算的是1970年以来的秒数
./read_cold -f ../db/ali_single_valid_index/${idf_num}.txt -k 16 -v 64 -d ../ssd/db_${idf_num}_k16 -m 7 -u -n 10000 -i 5 $* > ../evaluation/db_${idf_num}_k16_llsm_level.txt
end_time_run_level=$(date +%s)
cost_time_run_level=$((end_time_run_level-start_time_run_level))
formatted_time=$(date "+%Y-%m-%d %H:%M:%S")

cost_time_run=$((cost_time_run_baseline+cost_time_run_file+cost_time_run_level))
echo "${idf_num} : load ${cost_time_load} sec; run ${cost_time_run} sec, bl-file-level: ${cost_time_run_baseline}-${cost_time_run_file}-${cost_time_run_level} sec, at $formatted_time" >> $time_record_file
